package com.ylbh.modules.userclass.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ylbh.common.exception.NormalException;
import com.ylbh.common.pojo.business.dto.CashWithdrawalDTO;
import com.ylbh.common.pojo.business.vo.BusinessInformationExcelVO;
import com.ylbh.common.pojo.business.vo.CashWithdrawalDetailsVO;
import com.ylbh.common.pojo.business.vo.CashWithdrawalListVO;
import com.ylbh.common.pojo.icon.dto.AddIconDTO;
import com.ylbh.common.pojo.icon.dto.IconDTO;
import com.ylbh.common.pojo.icon.vo.IconDetailsVO;
import com.ylbh.common.pojo.icon.vo.IconImgVO;
import com.ylbh.common.pojo.icon.vo.IconListVO;
import com.ylbh.common.pojo.userclass.dto.BusinessInformationDTO;
import com.ylbh.common.pojo.userclass.dto.UpdateBusinessInformationDTO;
import com.ylbh.common.pojo.userclass.vo.BusinessClassListVO;
import com.ylbh.common.pojo.userclass.vo.BusinessDetailsVO;
import com.ylbh.common.pojo.userclass.vo.BusinessInformationVO;
import com.ylbh.common.utils.ExcelUtils;
import com.ylbh.common.utils.PageDTO;
import com.ylbh.modules.userclass.entity.ShoppingUserClass;
import com.ylbh.modules.userclass.mapper.ShoppingUserClassMapper;
import com.ylbh.modules.userclass.service.ShoppingUserClassService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ylbh.common.utils.PageUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@Service
public class ShoppingUserClassServiceImpl extends ServiceImpl<ShoppingUserClassMapper, ShoppingUserClass> implements ShoppingUserClassService {

    @Autowired
    private ShoppingUserClassMapper shoppingUserClassMapper;

    private final String shoppingMainImage = "http://image.yilianbaihui.cn/consumer/";

    /**
     * 联盟商家用户分类信息列表
     * @param page
     * @param limit
     * @return
     */
    @Override
    public PageUtils queryClassification(int page, int limit) {
        Page<ShoppingUserClass> page1 = new Page<>(page, limit);
        List<ShoppingUserClass> lists = shoppingUserClassMapper.selectClassification(page1);
        Page<ShoppingUserClass> page2 = new Page<ShoppingUserClass>().setTotal(page1.getTotal()).setRecords(lists);
        return new PageUtils(page2);
    }

    /**
     * 根据id查看详情
     * @param id
     * @return
     */
    @Override
    public String getClassNameById(Long id) {
        return shoppingUserClassMapper.selectClassNameById(id);
    }

    /**
     * 根据id修改分类信息
     * @param id
     * @param className
     */
    @Override
    public void modifyByIds(Long id,String className) {
        shoppingUserClassMapper.updateByIds(id,className);
    }

    @Override
    public void deleteByIds(String id) {
        shoppingUserClassMapper.deleteByIds(id);
    }

    /**
     * 新增分类
     * @param className
     */
    @Override
    public void addClass(String className) {
        shoppingUserClassMapper.insertClass(className,new Date());
    }

    /**
     * 联盟店信息管理列表
     * @param informationListList
     * @return
     */
    @Override
    public PageUtils queryBusinessInformation(BusinessInformationDTO informationListList) {
        Page<Object> page = new Page<>(informationListList.getPage(), informationListList.getLimit());
        List<BusinessInformationVO> list = shoppingUserClassMapper.selectBusinessInformation(informationListList, page);
        return new PageUtils(new Page<BusinessInformationVO>().setTotal(page.getTotal()).setRecords(list));
    }

    /**
     * 根据id查看联盟店信息管理详情信息
     * @param id
     * @return
     */
    @Override
    public BusinessDetailsVO getBusinessInformationById(Long id) {
        List<BusinessClassListVO> classList = shoppingUserClassMapper.selectBusinessClassList();
        BusinessDetailsVO find = shoppingUserClassMapper.selectBusinessInformationById(id);
        find.setList(classList);
        return find;
    }

    /**
     * 根据id修改联盟店信息管理详情信息
     * @param updateByIdOrInformationDTO
     */
    @Override
    public void modifyClassName(UpdateBusinessInformationDTO updateByIdOrInformationDTO) {
        shoppingUserClassMapper.updateClassName(updateByIdOrInformationDTO);
    }

    /**
     * 导出联盟店信息管理
     * @param response
     */
    @Override
    public void businessInformationExcel(HttpServletResponse response, HttpServletRequest request) throws NormalException {
        List<BusinessInformationExcelVO> excel = shoppingUserClassMapper.selectBusinessInformationExcel(request.getParameter("keywords"), request.getParameter("userType"));
        ExcelUtils.exportExcel(excel, "联盟店信息管理", "", BusinessInformationExcelVO.class, System.currentTimeMillis() + ".xls", response);
    }

    /**
     * 联盟店提现列表
     * @param cashWithdrawalDTO
     * @return
     */
    @Override
    public PageUtils queryBusinessCashWithdrawal(CashWithdrawalDTO cashWithdrawalDTO) {
        Page<Object> page = new Page<>(cashWithdrawalDTO.getPage(), cashWithdrawalDTO.getLimit());
        List<CashWithdrawalListVO> withdrawal = shoppingUserClassMapper.selectBusinessCashWithdrawal(cashWithdrawalDTO, page);
        Page<CashWithdrawalListVO> page1 = new Page<>();
        page1.setTotal(page.getTotal()).setRecords(withdrawal);
        return new PageUtils(page1);
    }

    /**
     * 根据id查看联盟店提现详情
     * @param id
     * @return
     */
    @Override
    public CashWithdrawalDetailsVO getBusinessCashWithdrawal(Long id) {
        CashWithdrawalDetailsVO find = shoppingUserClassMapper.selectByIdOrCashWithdrawal(id);
        if (null != find)
            if (!StringUtils.isEmpty(find.getLicensePic()))
                find.setLicensePic(shoppingMainImage + find.getLicensePic());
            if (!StringUtils.isEmpty(find.getName()))
                find.setMainImgSrc(shoppingMainImage + find.getPath() + "/" + find.getName());
            else {
                find.setMainImgSrc(shoppingMainImage + find.getPath());
            }
        return find;
    }

    /**
     * icon图标管理列表
     * @param pageDTO
     * @return
     */
    @Override
    public PageUtils queryIconList(PageDTO pageDTO) {
        Page<Override> page = new Page<>(pageDTO.getPage(), pageDTO.getLimit());
        List<IconListVO> iconList = shoppingUserClassMapper.selectIconList(page);
//        for (IconListVO i:iconList) {
//            if (null != i.getIconAccId()) {
//                IconImgVO pic = shoppingUserClassMapper.selectIconImg(i.getIconAccId());
//                if (null != pic) {
//                    if (!StringUtils.isEmpty(pic.getName()) && !StringUtils.isEmpty(pic.getPath()))
//                        i.setImg("http://image.yilianbaihui.cn/consumer/" + pic.getPath() + "/" + pic.getName());
//                }
//            }
//        }
        Page<IconListVO> page1 = new Page<>();
        page1.setTotal(page.getTotal()).setRecords(iconList);
        return new PageUtils(page1);
    }

    /**
     * icon图标详情
     * @param id
     * @return
     */
    @Override
    public IconDetailsVO getIconById(Long id) {
        IconDetailsVO info = shoppingUserClassMapper.selectIconById(id);
        return info;
    }

    /**
     * icon图标修改
     * @param iconDTO
     */
    @Override
    public void modifyIcon(IconDTO iconDTO) {
        if (iconDTO.getImg().get(0).indexOf("/consumer/icon/")!= -1) {
            String substring = iconDTO.getImg().get(0).substring(iconDTO.getImg().get(0).indexOf("consumer/icon/"));
            String path = "icon";
            String name = substring.substring(substring.lastIndexOf("/")+1);
            iconDTO.setImgName(name);
            iconDTO.setImgPath(path);
            shoppingUserClassMapper.updateIconDetails(iconDTO);
        }
    }

    /**
     * 新增金刚位及icon图标
     * @param iconDTO
     */
    @Override
    public void addIcon(IconDTO iconDTO) {
        if (iconDTO.getImg().get(0).indexOf("/consumer/icon/")!= -1) {
            String substring = iconDTO.getImg().get(0).substring(iconDTO.getImg().get(0).indexOf("consumer/icon/"));
            String name = substring.substring(substring.lastIndexOf("/")+1);
            String path = "icon";
            iconDTO.setImgPath(path);
            iconDTO.setImgName(name);
            iconDTO.setAddTime(new Date());
            shoppingUserClassMapper.insertIconClass(iconDTO);
        }
    }

}
